angular.module('controller.host', ['service.host'])
    .controller('hostCtrl', function ($scope, hostService) {
        $scope.formBean = {
        };

        $scope.submit = function(){
        	$('.popsHost .popsTit a').trigger("click")
         window.location.reload();
            hostService.post($scope.formBean,function (data) {
            });
        }

      $scope.templateTag='all';
      $scope.pageindex = 1
      $scope.pagesize = 1
      $scope.hostnamesearch;

        $scope.selectTag = function(tag){
            $scope.templateTag = tag;
        }

        $scope.templateFilter = function (item) {
            if ($scope.templateTag == 'all')
                return true;
            for (var i in item.templateTag) {
          
                if (item.templateTag[i] == $scope.templateTag) return true;
            }
            return false;
        };

        $scope.cpuchange = function (count) {
            $scope.formBean.cpunumber = count;
        }

        $scope.templatechange = function (template) {
            $scope.formBean.templatename = template.name;
            $scope.formBean.templateid = template.id;
        }

        $scope.memorychange = function (memory) {
            $scope.formBean.memory = memory;
            console.log(memory);
        }
        $scope.performancechange = function (performancename) {
            $scope.formBean.performancename = performancename
        }

        $scope.networkchange = function(network){
            $scope.formBean.networkids = network.id;
            $scope.formBean.networkname = network.name;
        }

        hostService.get( $scope.pageindex,$scope.pagesize,$scope.hostnamesearch,function (data) {
            $scope.hosts = data;
        });
        hostService.template(function (data) {
            $scope.template = data;
        });
        hostService.networks(function (data) {
            $scope.networks = data;
        });
       
        $scope.indexpage = function(page,hostnamesearch){
        	hostService.get(page,$scope.pagesize,hostnamesearch,function (data) {
        		$scope.pageindex = page
        		$scope.hosts = data;
        	});
        }

        $scope.prepage = function(page,hostnamesearch){
        	page = page-1<0?1:page
        	hostService.get(page, $scope.pagesize,hostnamesearch,function (data) {
        		 $scope.pageindex = page
                $scope.hosts = data;
            });
        }
        $scope.nextpage = function(page,count,hostnamesearch){
        	page = page>(parseInt((count - 1) / $scope.pagesize)) + 1?parseInt(((count - 1) / $scope.pagesize))+1:page
        	hostService.get(page,$scope.pagesize,hostnamesearch,function (data) {
        		 $scope.pageindex = page
                $scope.hosts = data;
            });
        }
        
        $scope.endpage = function(count,hostnamesearch){
        	page = parseInt(((count-1)/$scope.pagesize))+1;
        	hostService.get(page,$scope.pagesize,hostnamesearch,function (data) {
        		$scope.pageindex = page
        		$scope.hosts = data;
        	});
        }

        //表单步骤控制
        //当前步骤数
        $scope.step = 1;
        //第1步数据完整性验证
        $scope.validateStep1 = function(){
            return $scope.step==1&&$scope.formBean.templateid;
        }
        //第2步数据完整性验证
        $scope.validateStep2 = function(){
            return $scope.step==2&&$scope.formBean.cpunumber&&$scope.formBean.memory;
        }

        //第3步数据完整性验证
        $scope.validateStep3 = function(){
            return $scope.step==3&&$scope.formBean.networkids;
        }

        //下一步
        $scope.next = function(){
            if($scope.validateStep1()||$scope.validateStep2()||$scope.validateStep3()){
                $scope.step++;
                var amount = -600*($scope.step-1);
                $('.popsHost_all').animate({left: amount});

            }
        }

        //上一步
        $scope.pre = function(){

            $scope.step--;
            var amount = -600*($scope.step-1);
            $('.popsHost_all').animate({left: amount});
        }
        
    });
